/**
 * Created by Neville Kadwa.
 */
package org.shiftone.cdep.browser.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.shiftone.cdep.browser.client.DataService;
import org.shiftone.cdep.browser.client.types.CdepClass;
import org.shiftone.cdep.store.StoreManager;
import org.shiftone.cdep.store.StoreManagerFactory;
import org.shiftone.cdep.store.StoreException;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class DataServiceImpl extends RemoteServiceServlet implements DataService
{
    public static final Logger LOG = Logger.getLogger(DataServiceImpl.class);

    protected void onAfterResponseSerialized(String string)
    {
//        LOG.info("onAfterResponseSerialized: " + string);
    }

    protected void onBeforeRequestDeserialized(String string)
    {
//        LOG.info("onBeforeRequestDeserialized: " + string);
    }

    public CdepClass[] getClassNames() throws StoreException
    {

//        try
//        {
            StoreManager storeManager = StoreManagerFactory.getStoreManager();

            List classDeps = storeManager.queryClasses();

            CdepClass[] results = new CdepClass[classDeps.size()];

            Iterator iter = classDeps.iterator();
            for (int i = 0; iter.hasNext(); i++)
            {
                Map classEntry = (Map) iter.next();
                results[i] = new CdepClass(DataTypesUtil.getInt(classEntry, "CLASS_ID"),
                                           DataTypesUtil.getString(classEntry, "NAME_TX"),
                                           DataTypesUtil.getBool(classEntry, "INTERFACE_IN"),
                                           DataTypesUtil.getInt(classEntry, "SUPER_ID"));
            }

            LOG.info("Returning " + results.length + " Class Names from server.");
            return results;

//        }
//        catch (StoreException sex)
//        {
//            return new String[] {
//                    "Store Exception", sex.getMessage(), sex.getLocalizedMessage()
//            };
//        }

    }

}
